(function() {
    angular.module('BlurAdmin.pages.SystemSettings')
        .controller('IntegralRuleCtrl', IntegralRuleCtrl);

    function IntegralRuleCtrl($scope, $mydialog, $openModal, $http, $myconfig) {
        //弹窗框
        $scope.open = $openModal; //创建实例化对象
        function fn(val) { //关闭模态框方法
            $scope.myopen.opt_close = val;
        }
        $scope.myopen = function(page, size) { //打开模态框
            $scope.open.newOpen(page, size, $scope, fn);
        }


        //获取系统信息
        function get_list() {
            $http.get($myconfig.api.app_list_integralrule_b).then(function(res) {
                if (res.data.success) {
                    console.log(res);
                    $scope.list_data = res.data.data;

                }
            });
        }
        get_list();

        $scope.$watch('integralrule', function(newVal, oldVal) {
            console.log(newVal);
            console.log(oldVal);
            if ($scope.integralrule.limit) {
                $scope.integralrule.islimit = true; //有每日上下次数
                $scope.integralrule.cycletime = '1' //循环周期一天
            } else {
                $scope.integralrule.islimit = false; //无限制
                $scope.integralrule.cycletime = '0'
            }
        }, true);


        //编辑健康豆
        $scope.bianji = function(opt) {
            var a = JSON.stringify(opt);
            $scope.integralrule = JSON.parse(a);
            $http.get($myconfig.api.app_integralrule_info + "?rule_id=" + opt.id + "&rule_name=" + opt.name).then(function(res) {
                $scope.integralrule = res.data.data;

            });
            $scope.myopen('app/pages/SystemSettings/IntegralRule/EditPoints.html', 'lg');
        }

        $scope.integralrule = {
            "name": "", //健康豆规则名称
            // "alias": "", //
            "num": "", //单次健康豆
            "limit": "", //上限次数
            "islimit": false, //
            "cycletime": "", //循环周期 0无 1天
            "describe": "", //任务描述
            "missiondesc": "" //任务说明
        }

        //选择无限制健康豆
        $scope.radio_cl = function() {
            $scope.integralrule.limit = "";
            $scope.integralrule.cycletime = '0'
            $scope.integralrule.islimit = false;
        }

        var opt = {};
        $scope.shezhi = function() {
            opt = $scope.integralrule;
            if (!opt.num || isNaN(opt.num) || opt.num < 0) {
                return $mydialog.fail('请添加单次健康豆');
            }

            if (opt.islimit && !opt.limit) {
                return $mydialog.fail('请添加上限次数');
            }

            if (opt.islimit && ((opt.limit && isNaN(opt.limit)) || opt.limit < 0)) {
                return $mydialog.fail('请添加正确的上限次数');
            }

            if (!opt.describe) {
                return $mydialog.fail('请添加任务描述');
            }
            if (!opt.missiondesc) {
                return $mydialog.fail('请添加任务说明');
            }
            jiance();
        }

        function jiance() {
            var opt = {};
            opt = $scope.integralrule;
            opt.rule_id = $scope.integralrule.id;
            opt.name = $scope.integralrule.name;
            opt.limit = $scope.integralrule.limit;
            opt.islimit = $scope.integralrule.islimit ? true : false;

            opt.cycletime = $scope.integralrule.cycletime;
            opt.describe = $scope.integralrule.describe;
            opt.missiondesc = $scope.integralrule.missiondesc;

            if (!opt.islimit) {
                delete opt.limit;
            }

            $http.post($myconfig.api.app_integralrule_update_, opt).then(function(res) {
                if (res.data.success) {
                    $mydialog.success(res.data.date);
                    get_list();

                    $scope.myopen.opt_close();
                }
            })
        }



    }
})();